<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
* @author Radu Buzea
* @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
* @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
* @package HypnoPHP
* @category Core
* @version 1.0.3
*/

defined('HYPNOPHP_APPLICATION') or
die("Cannot use <i>" . basename(__FILE__) . "</i> outside HypnoPHP Application");

// ============================================================================
// Final class: HCollection
// ============================================================================

final class HCollection
{

	protected $__properties = array();

	# ###########################################################################

	/**
	* Class constructor
	* @return HCollection
	*/
	public function __construct()
	{
		$this->__properties = array();
	}

	# ###########################################################################

	/**
	* Global members overloading - Getter method
	*
	* @param string $param The object property name
	*/
	public function __get($param = '')
	{
		$param = strtolower(trim($param));
		if ($param != '' && array_key_exists($param, $this->__properties)) {
			return $this->__properties[$param];
		}
		return null;
	}

	# ###########################################################################

	/**
	* Global members overloading - Setter method
	*
	* @param string $param The object member name
	* @param mixed $value The object member value
	*/
	public function __set($param = '', $value = null)
	{
		$param = trim($param);
		if ($param == '') { return; }
		$this->__properties[$param] = $value;
	}

	# ###########################################################################

	/**
	* Check if a member exists
	*
	* @param string $param The object member name
	* @return bool
	*/
	public function __isset($param = '')
	{
		$param = strtolower($param);
		return isset($this->__properties[$param]);
	}

	# ###########################################################################

	/**
	* Check if a member is inaccessible
	*
	* @param string $param The object member name
	*/
	public function __unset($param = '')
	{
		$param = strtolower($param);
		if (isset($this->__properties[$param])) { unset($this->__properties[$param]); }
	}

	# ###########################################################################

	/**
	* Class to string conversion
	*
	* @return string
	*/
	public function __toString()
	{
		$result = '[';
		$is_first = true;
		foreach ($this->__properties as $key => $value) {
			if ($is_first) { $is_first = false; }
			else { $result .= ', '; }
			$result .= "'$key' => ";
			switch (gettype($value)) {
				case 'boolean':
					$result .= $value ? 'TRUE' : 'FALSE';
					break;
				case 'integer':
				case 'string':
					$result .= $value;
					break;
				default:
					$result .= '[' . ucwords(gettype($value)) . ']';
			}
		}
		$result.= ']';
	}

	# ###########################################################################

	/**
	* Assign one or more values to the collection
	*
	* @return bool
	*/
	public function assign()
	{
		$anum = func_num_args();
		if ($anum > 0) {
			$arg = func_get_arg(0);
			if (is_array($arg) && count($arg) > 0) {
				$this->__properties = array_change_key_case(array_merge($this->__properties, $arg), CASE_LOWER);
				return true;
			} else if (is_string($arg) && $anum > 1) {
				$value = func_get_arg(1);
				$this->__properties[$arg] = $value;
				return true;
			}
		}
		return false;
	}

	# ###########################################################################

	/**
	* Returns a list of stored properties
	*
	* @return array
	*/
	public function toArray()
	{
		return $this->__properties;
	}

	# ###########################################################################

}
